﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using PSDLib.Models;
using PSDLib.Protocols;
using PSDManager.SystemSettings;
using System.Net;
using PSDLib;

namespace PSDManager
{
    public partial class BurnTaskPanel : UserControl, IDataPanel
    {
        private const int cdRomInfoMargin = 5;
        private delegate void SetCDRomNameEventHandler(CDRomInfo cdrom, string name);
        private SetCDRomNameEventHandler setCDRomName;

        public ManagerHelper ManagerHelper { get; set; }
        private bool requesting = false;
        private Thread burnThread;
        private BurnRuntime burnRuntime;
        public BurnTaskPanel()
        {
            InitializeComponent();

            this.Resize += BurnTaskPanel_Resize;
            this.lvwDVDList.SelectedIndexChanged += LvwDVDList_SelectedIndexChanged;

            setCDRomName = new SetCDRomNameEventHandler(delegate (CDRomInfo cdrom, string name)
            {
                cdrom.SetRecorderName(name);
            });
        }



        private void BurnTaskPanel_Resize(object sender, EventArgs e)
        {
            int w = (this.palCDRom.Width - this.palCDRom.Padding.Left - this.palCDRom.Padding.Right - this.palSeperator.Width) / 2;

            this.cdRomInfo1.Width = w;

            this.cdRomInfo2.Width = w;

            this.palDVDList.Width = w;

            this.palFileList.Width = w;

            this.Refresh();
        }

        public void RefreshData(params object[] parameter)
        {
            if (burnThread == null)
            {
                requesting = true;
                burnThread = new Thread(new ThreadStart(RequestBurnRumtime));
                burnThread.Start();
            }
        }

        public void Reset(bool immediate)
        {
            if (immediate)
            {
                cdRomInfo1.SetRecorderName("");
                cdRomInfo2.SetRecorderName("");
            }
        }

        /// <summary>
        /// 定时请求刻录运行时信息
        /// </summary>
        private void RequestBurnRumtime()
        {
            while (true)
            {
                try
                {
                    if (ManagerHelper.CurrPSD != null && requesting)
                    {
                        if (ManagerHelper.CurrPSD.DeviceInfo.RecorderList.Count > 0)
                        {
                            this.Invoke(setCDRomName, cdRomInfo2, ManagerHelper.CurrPSD.DeviceInfo.RecorderList[0]);
                        }
                        else
                        {
                            this.Invoke(setCDRomName, cdRomInfo2, "");
                        }

                        if (ManagerHelper.CurrPSD.DeviceInfo.RecorderList.Count > 1)
                        {
                            this.Invoke(setCDRomName, cdRomInfo1, ManagerHelper.CurrPSD.DeviceInfo.RecorderList[1]);
                        }
                        else
                        {
                            this.Invoke(setCDRomName, cdRomInfo1, "");
                        }

                        var msg = new RequestBurnRuntimeMessage();

                        msg.ManagerPort = AppSettings.Setting.PDMListenPort;

                        var deviceEP = new IPEndPoint(IPAddress.Parse(ManagerHelper.CurrPSD.DeviceInfo.device_ip), ManagerHelper.CurrPSD.DeviceInfo.device_port);

                        ManagerHelper.TcpManager.Send(deviceEP, msg);
                    }

                    Thread.Sleep(5000);
                }
                catch (Exception ex)
                { }
            }
        }

        /// <summary>
        /// 更新刻录运行时信息
        /// </summary>
        /// <param name="runtime"></param>
        public void UpdateBurnRuntime(BurnRuntime runtime)
        {
            if (runtime != null)
            {
                try
                {
                    burnRuntime = runtime;

                    lvwDVDList.Items.Clear();

                    BurnJob job1 = null;
                    BurnJob job2 = null;

                    foreach (var job in runtime.BurnJobs)
                    {
                        var item = new ListViewItem();
                        item.Text = (lvwDVDList.Items.Count + 1).ToString();
                        item.SubItems.Add(job.DVD.dvd_title);
                        item.SubItems.Add(Enum.ToObject(typeof(ResultCode), job.State).ToString());
                        item.Tag = job;

                        lvwDVDList.Items.Add(item);

                        if (job.State == (int)ResultCode.BurnJobWorking)
                        {
                            if (cdRomInfo1.RecorderName == job.RecorderName)
                            {
                                job1 = job;
                            }
                            else if (cdRomInfo2.RecorderName == job.RecorderName)
                            {
                                job2 = job;
                            }
                        }
                    }

                    cdRomInfo1.SetBurnJob(job1);
                    cdRomInfo2.SetBurnJob(job2);

                    if (lvwDVDList.Items.Count > 0)
                    {
                        lvwDVDList.Items[0].Selected = true;
                    }
                    else
                    {
                        lvwFileList.Items.Clear();
                    }
                }
                catch (Exception ex)
                { }
            }
        }

        private void LvwDVDList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (burnRuntime != null)
            {
                try
                {
                    BurnJob job = (lvwDVDList.SelectedItems[0].Tag as BurnJob);

                    lvwFileList.Items.Clear();

                    foreach (var f in job.Files)
                    {
                        var item = new ListViewItem();
                        item.Text = (lvwFileList.Items.Count + 1).ToString();
                        item.SubItems.Add(f.file_name);
                        item.SubItems.Add(PSDFunction.FormatByteSize(f.file_size));
                        item.Tag = f;

                        lvwFileList.Items.Add(item);
                    }
                }
                catch (Exception ex)
                { }
            }
        }
    }
}
